package etnorservice.etnor_service.controller;


import com.github.pagehelper.PageInfo;
import etnorservice.etnor_service.domain.dto.ArticleQueryParam;
import etnorservice.etnor_service.domain.po.Article;
import etnorservice.etnor_service.domain.po.Category;
import etnorservice.etnor_service.domain.po.Tag;
import etnorservice.etnor_service.domain.po.UserInfo;
import etnorservice.etnor_service.result.Result;
import etnorservice.etnor_service.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

@RestController
public class BlogController {

    private static final Logger logger = LoggerFactory.getLogger(BlogController.class);

    @Autowired
    private BlogService blogService;

    /**
     * 获取博客首页数据
     */
    @GetMapping("/blog/home")
    public Result<Map<String, Object>> getHomeData() {
        try {
            Map<String, Object> data = new HashMap<>();

            // 获取精选文章
            ArticleQueryParam featuredParam = new ArticleQueryParam();
            featuredParam.setIsFeatured(true);
            featuredParam.setPageSize(5);
            PageInfo<Article> featuredArticles = blogService.getArticleList(featuredParam);

            // 获取最新文章
            ArticleQueryParam latestParam = new ArticleQueryParam();
            latestParam.setSortBy("latest");
            latestParam.setPageSize(10);
            PageInfo<Article> latestArticles = blogService.getArticleList(latestParam);

            // 获取热门标签
            List<Tag> hotTags = blogService.getHotTags(10);

            // 获取分类列表
            List<Category> categories = blogService.getCategoryList();

            data.put("featuredArticles", featuredArticles.getList());
            data.put("latestArticles", latestArticles.getList());
            data.put("hotTags", hotTags);
            data.put("categories", categories);

            return new Result<>(data);
        } catch (Exception e) {
            logger.error("获取首页数据失败", e);
            return Result.error("获取首页数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取文章列表
     */
    @GetMapping("/blog/articles")
    public Result<PageInfo<Article>> getArticleList(
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) Long tagId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String timeRange,
            @RequestParam(defaultValue = "latest") String sortBy,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        try {
            ArticleQueryParam param = new ArticleQueryParam();
            param.setCategoryId(categoryId);
            param.setTagId(tagId);
            param.setKeyword(keyword);
            param.setTimeRange(timeRange);
            param.setSortBy(sortBy);
            param.setPageNum(pageNum);
            param.setPageSize(pageSize);

            PageInfo<Article> pageInfo = blogService.getArticleList(param);
            return new Result<>(pageInfo);
        } catch (Exception e) {
            logger.error("获取文章列表失败", e);
            return Result.error("获取文章列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取文章详情
     */
    @GetMapping("/blog/articles/{id}")
    public Result<Article> getArticleDetail(@PathVariable Long id) {
        try {
            String content="";
            Article article = blogService.getArticleDetail(id);
            System.out.println(article);
            if (article == null) {
                return Result.error("文章不存在");
            }
            return new Result<>(article);
        } catch (Exception e) {
            logger.error("获取文章详情失败", e);
            return Result.error("获取文章详情失败：" + e.getMessage());
        }
    }

    /**
     * 获取分类列表
     */
    @GetMapping("/blog/categories")
    public Result<List<Category>> getCategoryList() {
        try {
            List<Category> categories = blogService.getCategoryList();
            return new Result<>(categories);
        } catch (Exception e) {
            logger.error("获取分类列表失败", e);
            return Result.error("获取分类列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取标签列表
     */
    @GetMapping("/blog/tags")
    public Result<List<Tag>> getTagList() {
        try {
            List<Tag> tags = blogService.getTagList();
            return new Result<>(tags);
        } catch (Exception e) {
            logger.error("获取标签列表失败", e);
            return Result.error("获取标签列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/blog/users/{id}")
    public Result<UserInfo> getUserInfo(@PathVariable Long id) {
        try {
            UserInfo userInfo = blogService.getUserInfo(id);
            if (userInfo == null) {
                return Result.error("用户不存在");
            }
            return new Result<>(userInfo);
        } catch (Exception e) {
            logger.error("获取用户信息失败", e);
            return Result.error("获取用户信息失败：" + e.getMessage());
        }
    }
}
